package com.ccut.xunfeiSpringboot.controller;/*
 * ClassName: DocumentController
 * Package: com.ccut.xunfeiSpringboot.controller
 * Description:
 * @Author 刘千湫
 * @Create 2025/6/2 22:17
 * @Version 1.0
 */

import com.ccut.xunfeiSpringboot.common.AjaxResult;
import com.ccut.xunfeiSpringboot.common.ApiResponse;
import com.ccut.xunfeiSpringboot.dto.SchoolResumeDTO;
import com.ccut.xunfeiSpringboot.dto.StudentResumeDTO;
import com.ccut.xunfeiSpringboot.service.DocumentProcessService;
import com.ccut.xunfeiSpringboot.utils.DtoToMapConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ccut.xunfeiSpringboot.utils.WordParserUtils;
import java.util.Map;

@RestController
@RequestMapping("/api/doc")
public class DocumentController {

    @Autowired
    private DocumentProcessService documentService;
    @Autowired
    private WordParserUtils wordParser;

    @PostMapping("/upload/student")
    public ApiResponse<Map<String, String>> uploadDocument(@RequestParam("file") MultipartFile file) {
        System.out.println("更新上传");
        try {
            // 解析 Word 获取数据
            AjaxResult result = wordParser.parseStudentDoc(file.getInputStream());

            StudentResumeDTO dto = (StudentResumeDTO) result.get("data");
            Map<String, String> data = DtoToMapConverter.convertStudent(dto);

            // 2. 更新数据库
            documentService.processStudentDocument(file);

            // 返回解析结果给前端用于自动填充表单
            return ApiResponse.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error("文档处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/upload/school")
    public ApiResponse<Map<String, String>> uploadSchoolDocument(@RequestParam("file") MultipartFile file) {
        System.out.println("更新");
        try {
            // 解析 Word 获取数据
            AjaxResult result = wordParser.parseSchoolDoc(file.getInputStream());
            // 提取数据中的 Map 部分用于响应
            SchoolResumeDTO dto = (SchoolResumeDTO) result.get("data");
            Map<String, String> data = DtoToMapConverter.convertSchool(dto);

            // 返回解析结果给前端用于自动填充表单
            return ApiResponse.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error("文档处理失败: " + e.getMessage());
        }
    }

}